package com.lb.dao.redis;

import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * Redis Key 过期事件消息指定数据库监听器
 * @author xiemingzhi
 * @since 2.0.0
 */
public class RedisKeyExpirationEventMessageDatabaseListener extends KeyExpirationEventMessageListener {

    private final PatternTopic keyeventExpiredDatabaseTopic;

    /**
     * Creates new {@link MessageListener} for {@code __keyevent@*__:expired} messages.
     *
     * @param listenerContainer must not be {@literal null}.
     */
    public RedisKeyExpirationEventMessageDatabaseListener(RedisMessageListenerContainer listenerContainer, int database) {
        super(listenerContainer);
        keyeventExpiredDatabaseTopic = new PatternTopic("__keyevent@*__:expired".replace("*", String.valueOf(database)));
    }

    @Override
    protected void doRegister(RedisMessageListenerContainer listenerContainer) {
        listenerContainer.addMessageListener(this, keyeventExpiredDatabaseTopic);
    }
}
